import base64
from datetime import datetime
from django.contrib.auth.models import AbstractUser
from django.db import models

# Create your models here.



class User(AbstractUser):
    """用户模型类"""

    GROUP_FOR_STAFF = (
        (1, 'DIY'),
        (2, 'JOOM'),
        (3, '1688'),
        (4, 'AMAZON'),
        (5, '其他')
    )
    staff_group = models.SmallIntegerField(choices=GROUP_FOR_STAFF, default=5, verbose_name='员工组别')
    mobile = models.CharField(max_length=11, default='', verbose_name='手机号')
    register_code = models.CharField(max_length=128, default='', verbose_name='注册码')


    class Meta:
        db_table = 'tb_users'
        verbose_name = '用户'
        verbose_name_plural = verbose_name


class RegisterCode(models.Model):
    register_code = models.CharField(max_length=128, default='', verbose_name='注册码')
    status = models.BooleanField(default=False, verbose_name='注册码状态')

    class Meta:
        db_table = 'tb_register_code'
        verbose_name = '用户'
        verbose_name_plural = verbose_name



"""良宝权限模型"""
class Role(models.Model):
    name = models.CharField(max_length=32, unique=True, verbose_name='角色名称')
    class Meta:
        db_table = 'tb_roles'
        verbose_name = '自定义角色'
        verbose_name_plural = verbose_name



class Menu(models.Model):
    name = models.CharField(max_length=32,unique=True, verbose_name='菜单名称')
    parent = models.ForeignKey(to='self', null=True,blank=True, verbose_name='上级菜单')
    class Meta:
        # unique_together = ('name', 'parent')
        db_table = 'tb_menus'
        verbose_name = '菜单'
        verbose_name_plural = verbose_name

class Permission(models.Model):
    url = models.CharField(max_length=32, verbose_name='路由URL')
    method = models.CharField(max_length=32, verbose_name='请求方法')
    detail = models.CharField(max_length=32, unique=True, verbose_name='权限简介')
    type = models.CharField(max_length=32, verbose_name='权限类型')


    class Meta:
        unique_together = ('url', 'method')
        db_table = 'tb_permissions'
        verbose_name = '自定义权限'
        verbose_name_plural = verbose_name




class UserRole(models.Model):
    user = models.ForeignKey('User')
    role = models.ForeignKey('Role')
    class Meta:
        unique_together = ('user', 'role')
        db_table = 'tb_users_roles'
        verbose_name = '用户分配角色'
        verbose_name_plural = verbose_name




class RolePermission(models.Model):
    role = models.ForeignKey('Role')
    permission = models.ForeignKey('Permission')

    class Meta:
        unique_together = ('role', 'permission')
        db_table = 'tb_roles_permissions'
        verbose_name = '角色分配权限'
        verbose_name_plural = verbose_name


class RoleMenu(models.Model):
    role = models.ForeignKey('Role')
    menu = models.ForeignKey('Menu')

    class Meta:
        unique_together = ('role', 'menu')
        db_table = 'tb_roles_menus'
        verbose_name = '角色分配菜单'
        verbose_name_plural = verbose_name